package com.dataseek.iot.device.manager.handler;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.core.constant.ResponseConst;
import com.dataseek.iot.core.handler.ServiceHandler;
import com.dataseek.iot.core.response.ResponseEntity;
import com.dataseek.iot.mysql.entity.ProductEvent;
import com.dataseek.iot.mysql.service.impl.ProductEventServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Objects;

/**
 * @author cc
 * @Title: 产品报警Handler
 * @Description: 产品报警Handler
 * @date 2021/9/2212:54 下午
 * @since 1.0
 */
@Service
public class ProductEventHandler extends ServiceHandler<ProductEvent> {

    @Autowired
    ProductEventServiceImpl productEventService;
    
    @Override
    public Mono<ResponseEntity> saveOne(Object entity) {
        ResponseEntity e = new ResponseEntity();
        QueryWrapper wrapper = new QueryWrapper();
        ProductEvent productEvent = (ProductEvent)entity;
        wrapper.eq("alarm_code", productEvent.getAlarmCode());
        if(isExist(wrapper)){
            e.setCode(ResponseConst.DUPLICATION_CODE);
            e.setMsg("事件编码已经存在");
            e.setData(productEvent.getAlarmCode());
            return Mono.just(e);
        }

        wrapper.or(true).eq("alarm_name", productEvent.getAlarmName());
        if(isExist(wrapper)){
            e.setCode(ResponseConst.DUPLICATION_CODE);
            e.setMsg("事件名称已经存在");
            e.setData(productEvent.getAlarmName());
            return Mono.just(e);
        }
        Boolean isSaved =  productEventService.save((ProductEvent) entity);
        return this.createResponse(isSaved);
    }

    @Override
    public Mono<ResponseEntity> saveBatch(List lists) {
        ResponseEntity e = new ResponseEntity();
        QueryWrapper wrapper = new QueryWrapper();
        for(Object o : lists){
            ProductEvent event = (ProductEvent) o;
            wrapper.eq("alarm_code", event.getAlarmCode());
            if(isExist(wrapper)){
                e.setCode(ResponseConst.DUPLICATION_CODE);
                e.setMsg("事件编码已经存在");
                e.setData(event.getAlarmCode());
                return Mono.just(e);
            }

            wrapper.or(true).eq("alarm_name", event.getAlarmName());
            if(isExist(wrapper)){
                e.setCode(ResponseConst.DUPLICATION_CODE);
                e.setMsg("事件名称已经存在");
                e.setData(event.getAlarmName());
                return Mono.just(e);
            }
        }
        Boolean isSaved =  productEventService.saveBatch(lists);
        return this.createResponse(isSaved);
    }


    @Override
    public Mono<ResponseEntity> removeById(String id) {
        Boolean isRemoved =  productEventService.removeById(id);
        return this.createResponse(isRemoved);
    }

    @Override
    public Mono<ResponseEntity> removeAll(List ids) {
        Boolean isRemoved = productEventService.removeByIds(ids);
        return this.createResponse(isRemoved);
    }

    @Override
    public Mono<ResponseEntity> remove(Wrapper wrapper) {
        Boolean isRemoved = productEventService.remove(wrapper);
        return this.createResponse(isRemoved);
    }

    @Override
    public Mono<ResponseEntity> remove(String id) {
        Boolean isRemoved = productEventService.removeById(id);
        return this.createResponse(isRemoved);
    }

    @Override
    public Mono<ResponseEntity> updateOne(Object entity) {
        Boolean isUpdated = productEventService.updateById((ProductEvent) entity);
        return this.createResponse(isUpdated);
    }

    @Override
    public Mono<ResponseEntity> update(Wrapper wrapper) {
        Boolean isUpdated = productEventService.update(wrapper);
        return this.createResponse(isUpdated);
    }

    @Override
    public Mono<ResponseEntity> updateBatch(List entities) {
        Boolean isUpdated = productEventService.updateBatchById(entities);
        return this.createResponse(isUpdated);
    }

    @Override
    public Mono<ResponseEntity> findOne(String id) {
        ProductEvent p = productEventService.getById(id);
        return this.createResponse(p);
    }

    @Override
    public Mono<ResponseEntity> page(Page p) {
        Page page = productEventService.page(p);
        return this.createResponse(page);
    }

    @Override
    public Mono<ResponseEntity> page(Page p, Wrapper wrapper) {
        Page page = productEventService.page(p, wrapper);
        return this.createResponse(page);
    }

    @Override
    public Flux<ResponseEntity> find(List ids) {
        List<ProductEvent> products = productEventService.listByIds(ids);
        return this.createResponse(products);
    }

    @Override
    public Flux<ResponseEntity> find(Wrapper wrapper) {
        List list = productEventService.list(wrapper);
        return this.createResponse(list);
    }

    @Override
    public Flux<ResponseEntity> findAll() {
        List list = productEventService.list();
        return this.createResponse(list);
    }

    @Override
    public Mono<ResponseEntity> defaultResponseOne(ResponseEntity responseEntity) {
        return Mono.just(responseEntity);
    }

    @Override
    public Flux<ResponseEntity> defaultResponseTwo(ResponseEntity responseEntity) {
        return Flux.just(responseEntity);
    }


    /**
     * 验证是否存在
     * @param wrapper
     * @return
     */
    public Boolean isExist(QueryWrapper wrapper){
        if(Objects.nonNull(productEventService.getOne(wrapper))) return true;
        return false;
    }

}
